package fun.qu_an.minecraft.voicechatplugin.mixin.tick_stasis;

import ax.arm32.minecraft.tickstasis.PauseManager;
import de.maxhenkel.voicechat.Voicechat;
import fun.qu_an.minecraft.voicechatplugin.Qu_anVoiceChatPlugin;
import net.minecraft.server.MinecraftServer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PauseManager.class)
public abstract class PauseManagerMixin {
	@Inject(method = "pauseServer(Ljava/lang/String;)V",
		remap = false,
		at = @At("TAIL"))
	private static void onPause(String reason, CallbackInfo ci) {
		MinecraftServer server = PauseManager.server;
		assert server != null;

		server.execute(() -> {
			Qu_anVoiceChatPlugin.LOGGER.info("Stopping voice chat server");
			Voicechat.SERVER.serverStopping(server);
		});
	}

	@Inject(method = "resumeServer(Ljava/lang/String;)V",
		remap = false,
		at = @At("TAIL"))
	private static void onResume(String reason, CallbackInfo ci) {
		MinecraftServer server = PauseManager.server;
		assert server != null;

		server.execute(() -> {
			Qu_anVoiceChatPlugin.LOGGER.info("Starting voice chat server");
			Voicechat.SERVER.serverStarting(server);
		});
	}
}
